/*
 *  Copyright (c) 2014 James Buncle
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 * 
 */
package com.jbuncle.seleniumiderunner;

import static com.jbuncle.seleniumiderunner.Utils.getHTMLDocument;
import static com.jbuncle.seleniumiderunner.Utils.getTitle;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;

/**
 *
 * @author James Buncle
 */
public class TestSuite {

    public static final long IMPLICITWAITSECONDS = 15;
    public static final long DEFAULTTIMEOUTSECTONS = 30;

    private final List<TestCase> testCases;
    private final String name;
    private final File directory;

    public TestSuite(final File suite) throws IOException {
        final Document doc = getHTMLDocument(suite);
        this.directory = suite.getParentFile();
        this.testCases = getTestCases(doc, this.directory);
        this.name = getTitle(doc);
    }

    public String getName() {
        return name;
    }

    public List<TestCase> getTestCases() {
        return new LinkedList<>(testCases);
    }

    public void run(final Iterable<WebDriver> webDrivers) {
        final List<TestDriver> drivers = new LinkedList<>();
        for (final WebDriver driver : webDrivers) {
            driver.manage().timeouts().implicitlyWait(IMPLICITWAITSECONDS, TimeUnit.SECONDS);
            drivers.add(new TestDriver(driver));
        }

        for (final TestCase testCase : this.testCases) {
            testCase.run(drivers);
        }
    }

    /**
     * Extract TestCases from links in the given Selenium HTML Suite Document,
     * relative to the given directory.
     *
     * @param suiteFile HTML Test Suite containing a table of links to HTML test
     * cases
     * @return list of TestCases as defined in HTML Suite
     * @throws IOException
     */
    private static List<TestCase> getTestCases(final Document htmlDocument, final File rootDirectory)
            throws IOException {
        final List<TestCase> testCases = new LinkedList<>();

        final Elements links = htmlDocument.select("table tbody tr a");
        for (final Element link : links) {
            final String href = link.attr("href");
            final String testName = link.text();
            testCases.add(new TestCase(new File(rootDirectory, href), testName));
        }
        return testCases;
    }

}
